package com.zzl.finance.data.controller.feign;

import com.zzl.apidoc.annotation.ApidocController;
import com.zzl.apidoc.annotation.ApidocOperation;
import com.zzl.apidoc.annotation.ApidocParam;
import com.zzl.finance.data.dto.EnterpriseAddDto;
import com.zzl.finance.data.dto.FinanceInstitutyAndProductDto;
import com.zzl.finance.data.dto.FinanceInstitutyIncludeProductDto;
import com.zzl.finance.data.dto.FinancialProductCalculateTicketDto;
import com.zzl.finance.data.dto.feign.BankAccountFeignAddDto;
import com.zzl.finance.data.entity.*;
import com.zzl.finance.data.manamger.EnterpriseManager;
import com.zzl.finance.data.manamger.FinanceInstitutyManager;
import com.zzl.finance.data.service.*;
import com.zzl.general.common.apidoc.annotation.ApidocResultKeyValR;
import com.zzl.general.common.apidoc.annotation.ApidocReturnPage;
import com.zzl.general.common.apidoc.annotation.ApidocReturnR;
import com.zzl.general.common.apidoc.constants.ResultTypeInR;
import com.zzl.general.common.ctrl.WebBaseCtrl;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.requestparameter.Ids;
import com.zzl.general.common.requestparameter.StringIds;
import com.zzl.general.common.utils.LogUtils;
import com.zzl.general.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author sunpp
 * @date 2021-11-29 13:17:46
 */
@ApidocController("基础数据服务feign接口")
@RestController
@RequestMapping("data/feign")
@Slf4j
public class FeignController extends WebBaseCtrl {

    @Autowired
    @SuppressWarnings("all")
    private FinanceInstitutyService financeInstitutyService;
    @Autowired
    @SuppressWarnings("all")
    private EnterpriseManager enterpriseManager;
    @Autowired
    @SuppressWarnings("all")
    private FinanceInstitutyManager financeInstitutyManager;
    @Autowired
    @SuppressWarnings("all")
    private FinancialProductService financialProductService;
    @Autowired
    @SuppressWarnings("all")
    private BankAccountService bankAccountService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 获取企业统一信用代码
     */
    @PostMapping("/getSocialCreditCode")
    public Map<String, String> getSocialCreditCode(@RequestBody StringIds ids) {
        return enterpriseService.getSocialCreditCode(ids.getIds());
    }

    /**
     * 通过企业id修改企业状态
     *
     * @param enterpriseEntity
     */
    @PostMapping("/updateEnterprise")
    @ApidocOperation(description = "通过企业id修改企业状态", operationId = "updateEnterprise/w2/1/2")
    @ApidocReturnR
    public void updateEnterprise(EnterpriseEntity enterpriseEntity) {
        enterpriseService.updateById(enterpriseEntity);
    }

    /**
     * 查找通讯录数据
     */
    @PostMapping("/getAddressBookByIds")
    @ApidocReturnPage(genericTypeClass = AddressBookEntity.class)
    public List<AddressBookEntity> getAddressBookByIds(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return new ArrayList<>();
        }
        return new ArrayList<>(addressBookService.listByIds(idList));
    }

    /**
     * 添加联系人 信息
     */
    @GetMapping("/list")
    @ApidocOperation(description = "分页查询数据", operationId = "addressBook1/q9")
    @ApidocReturnPage(genericTypeClass = AddressBookEntity.class)
    public Result list(@RequestParam("enterpriseId") String enterpriseId) {
        return Result.ok();
    }

    /**
     * 查询所有的 金融机构
     */
    @GetMapping("/listFinanceInstituty")
    @ApidocOperation(description = "分页查询数据", operationId = "addressBook1/w1")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_ARRAY, key = "list", valueClass = FinanceInstitutyEntity.class)
    })
    public List<FinanceInstitutyEntity> listFinanceInstituty() {
        return financeInstitutyService.list();
    }

    /**
     * 登录激活时，根据企业id查询法人的手机号
     */
    @PostMapping("/listLegalByEnterpriseIds")
    @ApidocOperation(description = "根据企业ids查询法人信息", operationId = "addressBook13/q4")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_ARRAY, key = "data", valueClass = EnterprisePersonEntity.class)
    })
    public List<EnterprisePersonEntity> listFinanceInstituty(@RequestBody StringIds ids) {
        return enterpriseManager.getLegalByEnterpriseId(ids.getIds());
    }

    /**
     * 通过企业ids获取企业信息
     */
    @PostMapping("/listEnterpriseByEnterpriseIds")
    @ApidocOperation(description = "通过企业ids获取企业信息", operationId = "addressBook13/w2")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_COLLOCTION, key = "data", valueClass = EnterpriseEntity.class)
    })
    public List<EnterpriseEntity> listEnterpriseByEnterpriseIds(@RequestBody StringIds ids) {
        return enterpriseManager.listByIds(ids.getIds());
    }

    @GetMapping("/getEnterpriseByEnterpriseId/{id}")
    @ApidocOperation(description = "通过企业id获取企业信息", operationId = "addressBook13/w2/1")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_COLLOCTION, key = "data", valueClass = EnterpriseEntity.class)
    })
    public EnterpriseEntity getEnterpriseByEnterpriseId(@PathVariable("id") String id) {
        return enterpriseService.getById(id);
    }

    /**
     * 企业名称和企业社会信用代码新增企业,返回企业id
     */
    @PostMapping("/saveEnterpriseReturnId")
    @ApidocOperation(description = "企业名称和企业社会信用代码新增企业,返回企业id", operationId = "addressBook13")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "data", valueClass = Integer.class)
    })
    public Result saveEnterpriseReturnId(@RequestBody EnterpriseAddDto enterpriseAddDto) {
        try {
            return Result.ok().data(enterpriseManager.saveEnterpriseReturnId(enterpriseAddDto));
        } catch (SysException e) {
            return Result.error("企业名称和企业社会信用代码新增企业,返回企业id失败！");
        }
    }

    /**
     * 企业名称模糊搜索企业
     */
    @GetMapping("/listByEnterpriseName/{enterpriseName}")
    @ApidocOperation(description = "企业名称模糊搜索企业", operationId = "/listByEnterpriseName/enterpriseName/q2")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_ARRAY, key = "data", valueClass = EnterpriseEntity.class)
    })
    public List<EnterpriseEntity> listByEnterpriseName(@ApidocParam(name = "enterpriseName", required = true) @PathVariable("enterpriseName") String enterpriseName) {
        return enterpriseManager.listByEnterpriseName(enterpriseName);
    }

    /**
     * 企业名称模糊搜索企业id
     */
    @GetMapping("/listIdsByEnterpriseName/{enterpriseName}")
    @ApidocOperation(description = "企业名称模糊搜索企业", operationId = "/listByEnterpriseName/enterpriseName/q3")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_ARRAY, key = "data", valueClass = EnterpriseEntity.class)
    })
    public List<String> listIdsByEnterpriseName(@ApidocParam(name = "enterpriseName", required = true) @PathVariable("enterpriseName") String enterpriseName) {
        return enterpriseManager.listIdsByEnterpriseName(enterpriseName);
    }

    /**
     * 通过金融机构ids查询金融机构
     */
    @PostMapping("/listFinanceInstitutyByIds")
    @ApidocOperation(description = "通过金融机构ids查询金融机构", operationId = "addressBook13/w3")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_ARRAY, key = "data", valueClass = FinanceInstitutyEntity.class)
    })
    public List<FinanceInstitutyEntity> listFinanceInstitutyByIds(@RequestBody Ids ids) {
        return financeInstitutyManager.listByIds(ids.getIds());
    }

    /**
     * 根据金融产品id和我的票据id计算融资数据
     * 包括预计放款金额、融资成本、融资成本组成等信息
     */
    @PostMapping("/financial/{productId}")
    @ApidocOperation(description = "根据金融产品id和我的票据id计算融资数据", operationId = "data/financialproduct/financial")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "financialProduct", valueClass = FinancialProductCalculateTicketDto.class)
    })
    public Result financialData(@ApidocParam(name = "金融产品id", required = true)
                                @PathVariable("productId") Integer productId,
                                @ApidocParam(name = "我的票据", required = true)
                                @RequestBody Map<String, Object> ticketMap) {
        try {
            return Result.ok().data(financialProductService.calcProduct(ticketMap, productId));
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 保存
     */
    @PostMapping("/saveBankAccountFeign")
    @ApidocOperation(description = "保存", operationId = "bankAccountSave1")
    @ApidocReturnR
    public Result save(@ApidocParam(name = "保存的对象值", required = true) @RequestBody BankAccountFeignAddDto accountAddDto) {
        try {
            return Result.ok().put("data", bankAccountService.saveBankAccountFeign(accountAddDto));
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    @GetMapping("/getBankAccountFeign/{id}")
    @ApidocOperation(description = "查询", operationId = "getBankAccountFeign")
    @ApidocReturnR
    public BankAccountEntity getBankAccountFeign(@ApidocParam(name = "查询银行账号对象值", required = true) @PathVariable(value = "id") String id) {
        return bankAccountService.getById(id);
    }

    /**
     * 通过金融机构ids查询金融机构及关联金融产品
     */
    @PostMapping("/listAllFinanceInstitutyAndProduct")
    @ApidocOperation(description = "通过金融机构ids查询金融机构及关联金融产品", operationId = "data/feign/listAllFinanceInstitutyAndProduct")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_ARRAY, key = "data", valueClass = FinanceInstitutyAndProductDto.class)
    })
    public List<FinanceInstitutyIncludeProductDto> listAllFinanceInstitutyAndProduct(@RequestBody Ids ids) {
        return financialProductService.listAllFinanceInstitutyAndProduct(ids.getIds());
    }

    /**
     * 通过金融机构ids查询金融机构及关联金融产品
     */
    @PostMapping("/calcInstitutyAndProduct")
    @ApidocOperation(description = "通过金融机构ids查询金融机构及关联金融产品", operationId = "data/financialproduct/listAllFinanceInstitutyAndProduct")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_ARRAY, key = "data", valueClass = FinanceInstitutyAndProductDto.class)
    })
    public Result calcInstitutyAndProduct(@RequestBody Map<String, Object> ticketMap) {
        try {
            return Result.ok().data(financialProductService.calcInstitutyAndProduct(ticketMap));
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 企业名称模糊搜索企业id
     */
    @GetMapping("/getCoreEnterpriseList")
    @ApidocOperation(description = "查询所有核心企业接口", operationId = "/listByEnterpriseName/{enterpriseName}")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_ARRAY, key = "data", valueClass = EnterpriseEntity.class)
    })
    public List<EnterpriseEntity> getCoreEnterpriseList() {
        return enterpriseService.getCoreEnterpriseList();
    }


    @PostMapping("/financial/product/{productId}")
    @ApidocOperation(description = "根据金融产品id查询产品信息", operationId = "data/financialproduct/financial/product")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "financialProduct", valueClass = FinancialProductCalculateTicketDto.class)
    })
    public FinancialProductEntity financialProductById(@ApidocParam(name = "金融产品id", required = true)
                                                       @PathVariable("productId") String productId) {
        try {
            return financialProductService.financialProductById(productId, this.getUser());
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return new FinancialProductEntity();
        }
    }

    @GetMapping("/getEnterpriseListByCategory/{category}")
    @ApidocOperation(description = "通过企业类型查询企业列表", operationId = "/listByEnterpriseName/getEnterpriseListByCategory/q2")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_ARRAY, key = "data", valueClass = EnterpriseEntity.class)
    })
    public List<EnterpriseEntity> getEnterpriseListByCategory(@ApidocParam(name = "category", required = true) @PathVariable("category") String category) {
        return enterpriseService.getEnterpriseListByCategory(category);
    }
}
